<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>双端队列数据结构</title>
</head>
<body>
	<script>
		class DeQue {
			constructor() {
				this.items = {}
				this.count = 0
				this.lowestCount = 0
			}
			isEmpty() {
				return this.lowestCount === this.count
			}
			size() {
				return this.count - this.lowestCount
			}
			toString() {
				if(this.isEmpty()) {
					return ''
				}
				let result = this.items[this.lowestCount]
				for(let i = this.lowestCount + 1; i < this.count; i++) {
					result = `${result},${this.items[i]}`
				}
				return result
			}
			addFront(element) {
				if(this.isEmpty()) {
					this.addBack(element)
				}else if(this.lowestCount > 0) {
					this.items[this.lowestCount - 1] = element
					this.lowestCount--
				}else {
					for(let i = this.count; i > 0; i--) {
						this.items[i] = this.items[i - 1]
					}
					this.items[0] = element
					this.count++
					this.lowestCount = 0
				}
			}
			addBack(element) {
				this.items[this.count] = element
				this.count++
			}
			removeFront() {
				if(this.isEmpty()) {
					return undefined
				}
				const result = this.items[this.lowestCount]
				delete this.items[this.lowestCount]
				this.lowestCount++
				return result
			}
			removeBack() {
				if(this.isEmpty()) {
					return undefined
				}
				const result = this.items[this.count - 1]
				delete this.items[this.count - 1]
				this.count--
				return result
			}
		}
	</script>
</body>
</html>